import os
import logging
from app.config import BaseConfig

def init_logger(conf: BaseConfig):
    print('>> init_logger')
    #### 日志配置
    logger = logging.getLogger()

    if conf.LOG_LEVEL.upper() == 'INFO':
        logger.setLevel(logging.INFO)
    elif conf.LOG_LEVEL.upper() == 'DEBUG':
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    if not os.path.exists(conf.LOG_DIR):
        os.makedirs(conf.LOG_DIR)
    print('LOG_DIR: %s' % conf.LOG_DIR)
    ch = logging.StreamHandler()
    log_file = os.path.join(conf.LOG_DIR, 'server.log')
    fh = logging.FileHandler(filename=log_file)
    formatter = logging.Formatter(
        "%(asctime)s - %(module)s - %(funcName)s - line:%(lineno)d - %(levelname)s - %(message)s"
    )
    
    ch.setFormatter(formatter)
    fh.setFormatter(formatter)
    logger.addHandler(ch) #将日志输出至屏幕
    logger.addHandler(fh) #将日志输出至文件